package com.pengliu.common.mybatis.generator;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.internal.DefaultCommentGenerator;

/**
 * user: pengliu
 * date: 2017年10月17日
 * desc: mybatis属性注释
 * @author pengliu
 */
public class SimpleCommentGenerator extends DefaultCommentGenerator {
	
	
	private static final String SETORDERBYCLAUSE = "setOrderByClause";
	
	private static final String ADDCRITERIA = "addCriteria";
	
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
        addJavadocTag(field, false);
        field.addJavaDocLine(" */");
    }
    /**
     * user: pengliu
     * date: 2017年10月17日
     * desc: 修改Model中的属性注释
     * @param method
     * @param introspectedTable
     * @param introspectedColumn
     * @author pengliu
     */
    @Override
    public void addGetterComment(Method method,IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {
        StringBuilder sb = new StringBuilder();
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * ");
        sb.append(" * 此方法返回【"+introspectedColumn.getRemarks()+"】的值. ");
        method.addJavaDocLine(sb.toString());
        method.addJavaDocLine(" *");
        sb.setLength(0);
        sb.append(" * @return 返回的值属性名称是: ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        sb.append('.');
        sb.append(introspectedColumn.getActualColumnName());
        method.addJavaDocLine(sb.toString());
        addJavadocTag(method, false);
        method.addJavaDocLine(" */");
    }
    /**
     * user: pengliu
     * date: 2017年10月17日
     * desc: 修改Model重点set注释
     * @param method
     * @param introspectedTable
     * @param introspectedColumn
     * @author pengliu
     */
    @Override
    public void addSetterComment(Method method,IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {
        StringBuilder sb = new StringBuilder();
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * ");
        sb.append(" * 该方法设置数据【"+introspectedColumn.getRemarks()+"】的值 ");
        method.addJavaDocLine(sb.toString());
        method.addJavaDocLine(" *");
        sb.setLength(0);
        sb.append(" * @param ");
        sb.append(" 为属性名称("+introspectedTable.getFullyQualifiedTable()+"."+introspectedColumn.getActualColumnName()+")设置值 ");
        sb.append('.');
        method.addJavaDocLine(sb.toString());
        addJavadocTag(method, false);
        method.addJavaDocLine(" */");
    }
    /**
     * user: pengliu
     * date: 2017年10月17日
     * desc: 修改Model中的Exampl属性名注释
     * @param field
     * @param introspectedTable
     * @author pengliu
     */
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
        StringBuilder sb = new StringBuilder();
        field.addJavaDocLine("/**");
        sb.append(" * 这字段对应的数据库表 ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        field.addJavaDocLine(sb.toString());
        addJavadocTag(field, false);
        field.addJavaDocLine(" */");
    }
    
    /**
     * user: pengliu
     * date: 2017年10月17日
     * desc: 修改Model中的Exampl方法注释
     * @param method
     * @param introspectedTable
     * @author pengliu
     */
    @Override
    public void addGeneralMethodComment(Method method,IntrospectedTable introspectedTable) {
        StringBuilder sb = new StringBuilder();
        method.addJavaDocLine("/**");
        sb.append(" * 此方法对应于数据库表 ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        method.addJavaDocLine(sb.toString());
        addJavadocTag(method, false);
        method.addJavaDocLine(" */");
        
        if(SETORDERBYCLAUSE.equals(method.getName())||ADDCRITERIA.equals(method.getName())) {
        	method.addJavaDocLine("@Override");
        }
    }

}
